var totalElements=0;
var fatherId = 0;
var pageNo = 1;
var pageSize=10;
/*******滚动条*******/
$("body").niceScroll({
    cursorcolor:"#888888",
    cursoropacitymax:1,
    touchbehavior:false,
    cursorwidth:"5px",
    cursorborder:"0",
    cursorborderradius:"5px"
});

$(function () {
    //分页调用
    pageLoad(fatherId,$("#totalElements").text());
    //返回图标
    $("#back").click(function () {
        history.back();
    });
    //前进图标
    $("#forward").click(function () {
        history.forward();
    });
    //添加城市样式
    $('#city_add').on('click', function(){
        $("input[name=cityName]").val("");
        $("input[name=cityEnName]").val("");
        $("input[name=cityAbbrCode]").val("");
        layer.open({
            type: 1,
            title:'添加城市/区县',
            area: ['600px',''],
            shadeClose: false,
            content: $('#add')
        });
        getProvince(0);
    });
    //添加操作
    $("#subAdd").click(function () {
        if (checkEmpty()) {
            if($(".chooseProvince_add option:selected").val()==0){
                layer.msg('省份是必选的哦！~', {
                    time: 2000, //2s后自动关闭
                });
                return false;
            }
            $.ajax({
                type: "POST",
                url: "/city/add",
                data: $("#form-city-add").serialize(),
                dataType: "json",
                success: function (data) {
                    if (data != null) {
                        layer.msg('添加成功！', {
                            time: 2000, //2s后自动关闭
                        });
                        setTimeout(function () {
                            location.reload();
                        },1000);
                    } else {
                        layer.msg('添加数据失败！请勿重复添加！', {
                            time: 2000, //2s后自动关闭
                        });
                    }
                },
                error: function () {
                    layer.msg('加载数据失败，请尝试刷新', {
                        time: 2000, //2s后自动关闭
                    });
                }
            });

        }
    });
    //修改操作
    $("#subUpdate").click(function () {
        if(checkEmpty()){
            if($(".chooseProvince_update option:selected").val()==0){
                layer.msg('省份是必选的哦！~', {
                    time: 2000, //2s后自动关闭
                });
                return false;
            }
            $.ajax({
                type: "POST",
                url: "/city/update",
                data: $("#form-city-update").serialize(),
                dataType: "json",
                success: function (data) {
                    if(data==true){
                        layer.msg('修改成功！', {
                            time: 2000, //2s后自动关闭
                        });
                        setTimeout(function () {
                            location.reload();
                        },1000);
                    }else{
                        layer.msg('修改失败！您修改的数据已存在！请勿填入重复数据！', {
                            time: 2000, //2s后自动关闭
                        });
                    }
                },
                error: function () {
                    layer.msg('修改失败，请尝试刷新后重试', {
                        time: 2000, //2s后自动关闭
                    });
                }
            });
        }
    });


    //改变省绑定新增的城市的下拉框
    $(".chooseProvince_add").change(function () {
        var province = $(".chooseProvince_add").val();
        if (province != "" && province != null) {
            $.ajax({
                type: "POST",
                url: "/city/hasFatherId",
                data: {fatherId: province},
                dataType: "json",
                success: function (data) {
                    $(".chooseCity_add").html("")
                    var options = "<option value='0'>--请选择--</option>";
                    for (var i = 0; i < data.length; i++) {
                        options += "<option value='" + data[i].cityId + "'>" + data[i].cityName + "</option>"
                    }
                    $(".chooseCity_add").html(options);
                },
                error: function () {
                    //配置一个透明的询问框
                    layer.msg('加载数据失败，请尝试刷新', {
                        time: 2000, //2s后自动关闭
                    });

                }
            });
        } else {
            $(".chooseCity_add").html("");
            var options = "<option value='0'>--请选择--</option>";
            $(".chooseCity_add").html(options);
        }
    });
    //改变省绑定修改的城市的下拉框
    $(".chooseProvince_update").change(function () {
        var province = $(".chooseProvince_update").val();
        if (province != "" && province != null) {
            $.ajax({
                type: "POST",
                url: "/city/hasFatherId",
                data: {fatherId: province},
                dataType: "json",
                success: function (data) {
                    $(".chooseCity_update").html("")
                    var options = "<option value='0'>--请选择--</option>";
                    for (var i = 0; i < data.length; i++) {
                        options += "<option value='" + data[i].cityId + "'>" + data[i].cityName + "</option>"
                    }
                    $(".chooseCity_update").html(options);
                },
                error: function () {
                    //配置一个透明的询问框
                    layer.msg('加载数据失败，请尝试刷新', {
                        time: 2000, //2s后自动关闭
                    });

                }
            });
        } else {
            $(".chooseCity_update").html("");
            var options = "<option value='0'>--请选择--</option>";
            $(".chooseCity_update").html(options);
        }
    });
    //点击省份展示城市
    $(".layui-nav").on("click", "li", function () {

            fatherId = $(this).children("span").text();
            location.href="/city/cityList?fatherId="+fatherId+"&pageNo="+parseInt($("#number").text()+1)+"&pageSize="+$("#pageSize").text();
            //添加样式
            $(this).addClass("layui-nav-itemed");

    });

});
//修改验证
function update(cityId,fatherId){
    if(fatherId==0){
        layer.msg('不可以修改任何省份哦！~', {
            time: 2000, //2s后自动关闭
        });
    }else{
        $.ajax({
            type: "POST",
            url: "/city/find",
            data: {cityId:cityId},
            dataType: "json",
            success: function (data) {
                if(data!=null){
                    $("input[name=cityName]").val(data.cityName);
                    $("input[name=cityEnName]").val(data.cityEnName);
                    $("input[name=cityAbbrCode]").val(data.cityAbbrCode);
                    $("input[name=cityId]").val(data.cityId);
                    getProvince(1);
                }
            },
            error: function () {
                layer.msg('获取数据失败', {
                    time: 2000, //2s后自动关闭
                });
            }
        });

        layer.open({
            type: 1,
            title:'修改城市/区县',
            area: ['600px',''],
            shadeClose: false,
            content: $('#update')
        });

    }
}
//删除数据
function del(cityId,fatherId){
    if(fatherId==0){
        layer.msg('不可以删除任何省份哦！', {
            time: 2000, //2s后自动关闭
        });
    }else{
        layer.confirm("确认要删除吗？",{btn:['确定','取消']},function () {
            $.ajax({
                type:"POST",
                url:"/city/del" ,
                data:{cityId:cityId},
                dataType:"json",
                success:function(data){
                    if(data=="true"){
                        layer.msg('删除成功！', {
                            time: 2000, //2s后自动关闭
                        });
                        setTimeout(function () {
                            location.reload();
                        },1000);
                    }
                },
                error:function () {
                    //配置一个透明的询问框
                    layer.msg('删除失败！尝试刷新后重试。', {
                        time: 2000, //2s后自动关闭
                    });
                }
            });
        },function () {
            layer.msg('用户取消操作', {
                time: 1500, //1.5s后自动关闭
            });
        });
    }
}

//分页
function pageLoad(fatherId,totalElements){
    layui.use(['laypage'], function(){
        var curr= 0;
        curr= parseInt($("#number").text())+1;
        var laypage = layui.laypage
        //执行一个laypage实例
        laypage.render({
            elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
            ,count: totalElements //数据总数，从服务端得到
            ,layout: ['count', 'prev', 'page', 'next', 'skip']
            ,curr:curr
            ,limit:10  //每一页显示的条数
            ,skip:true
            ,jump: function(obj, first) {

                //alert("执行查询数据，当前页码："+curr+"，页大小："+obj.limit+"，父id："+fatherId+"，总数："+totalElements)
                //obj包含了当前分页的所有参数，比如：
                console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                console.log(obj.limit); //得到每页显示的条数
                if(!first){
                    location.href="/city/cityList?fatherId="+fatherId+"&pageNo="+obj.curr+"&pageSize="+obj.limit;
                }
            }
        });
    });
}


//获取所有的省绑定到下拉框
function getProvince(bind) {
    $.ajax({
        type: "POST",
        url: "/city/hasFatherId",
        data: {fatherId: 0},
        dataType: "json",
        success: function (data) {
            var options = "<option value='0'>--请选择--</option>";
            for (var i = 0; i < data.length; i++) {
                options += "<option value='" + data[i].cityId + "'>" + data[i].cityName + "</option>"
            }
            if(bind==0){//绑添加城市的下拉框
                $(".chooseProvince_add").html(options);
                $(".chooseCity_add").html(options);
            }else{//绑修改城市的下拉框
                $(".chooseProvince_update").html(options);
                $(".chooseCity_update").html(options);
            }

        },
        error: function () {
            //配置一个透明的询问框
            layer.msg('加载数据失败，请尝试刷新', {
                time: 2000, //2s后自动关闭
            });
        }
    });
}


/**
 * 非空验证
 */
function checkEmpty() {
    //获取cityName
    if($("input[name=cityName]").val()==""||$("input[name=cityName]").val()==null){
        layer.msg('城市名称是必填的哦！~', {
            time: 2000, //2s后自动关闭
        });
        return false;
    }else if($("input[name=cityEnName]").val()==""||$("input[name=cityEnName]").val()==null){
        layer.msg('城市全拼是必填的哦！~', {
            time: 2000, //2s后自动关闭
        });
        return false;
    }else if($("input[name=cityAbbrCode]").val()==""||$("input[name=cityAbbrCode]").val()==null){
        layer.msg('城市首拼是必填的哦！~', {
            time: 2000, //2s后自动关闭
        });
        return false;
    }
    return true;
}

/*    //点击子级 阻止冒泡
$("ul").on("click", "dd", function (event) {
    event.stopPropagation();//阻止冒泡
});*/